package com.kc.sys.config;

import com.kc.sys.common.AppFileUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author forever_love
 * @create 2019--11--18 9:55
 **/
@Configuration
public class FileStaticConfig extends WebMvcConfigurerAdapter {

    //文件上传的保存路径
    public static  String UPLOAD_PATH="E:/upload/";//默认值E:/upload/
    static {
        //读取配置文件的存储地址
        InputStream stream = AppFileUtils.class.getClassLoader().getResourceAsStream("file.properties");
        Properties properties=new Properties();
        try {
            properties.load(stream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String property = properties.getProperty("filepath");
        if(null!=property) {
            UPLOAD_PATH=property;
        }
    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        /**
         *  /source/xxx   指文件的访问方式  如：localhost:8080/source/abc.wav
         *  file:d/voice/  指静态文件存放在服务器上的位置E:/upload/
         */

        registry.addResourceHandler("/source/**").addResourceLocations("file:"+UPLOAD_PATH);
    }
}
